package com.atguigu.jxc.controller;

import com.atguigu.jxc.domain.ServiceVO;
import com.atguigu.jxc.entity.Goods;
import com.atguigu.jxc.service.GoodsService;
import com.google.gson.JsonObject;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * @author fenge
 */
@Controller
@RequestMapping("goods")
public class GoodsController {

    @Autowired
    private GoodsService goodsService;

    @PostMapping("listInventory")
    @RequiresPermissions(value = "当前库存查询")
    @ResponseBody
    public Map<String,Object> listInventory(Integer page,
                                Integer rows,
                                @RequestParam(required = false) String codeOrName,
                                @RequestParam(required = false) Integer goodsTypeId){
        return goodsService.listInventory(page,rows,codeOrName,goodsTypeId);
    }

    @PostMapping("list")
    @ResponseBody
    public Map<String,Object> list(Integer page, Integer rows, String goodsName, Integer goodsTypeId){
        return goodsService.listInventory(page,rows,goodsName,goodsTypeId);
    }

    @PostMapping("save")
    @ResponseBody
    public ServiceVO save(Goods goods){
        goodsService.save(goods);
        return new ServiceVO(100,"请求成功");
    }

    @PostMapping("delete")
    @ResponseBody
    public ServiceVO delete(Integer goodsId){
        goodsService.delete(goodsId);
        return new ServiceVO(100,"请求成功");
    }

    @PostMapping("getNoInventoryQuantity")
    @ResponseBody
    public Map<String,Object> getNoInventoryQuantity(Integer page,Integer rows,String nameOrCode){
        return goodsService.getNoInventoryQuantity(page,rows,nameOrCode);
    }

    @PostMapping("getHasInventoryQuantity")
    @ResponseBody
    public Map<String,Object> getHasInventoryQuantity(Integer page,Integer rows,String nameOrCode){
        return goodsService.getHasInventoryQuantity(page,rows,nameOrCode);
    }

    @PostMapping("saveStock")
    @ResponseBody
    public ServiceVO saveStock(Integer goodsId,Integer inventoryQuantity,double purchasingPrice){
        goodsService.saveStock(goodsId,inventoryQuantity,purchasingPrice);
        return new ServiceVO(100,"请求成功");
    }

    @PostMapping("deleteStock")
    @ResponseBody
    public ServiceVO deleteStock(Integer goodsId){
        goodsService.delete(goodsId);
        return new ServiceVO(100,"请求成功");
    }

    @PostMapping("listAlarm")
    @ResponseBody
    public Map<String,Object> listAlarm(){
        return goodsService.listAlarm();
    }
}
